package com.aya.export.starter;

import com.aliyun.oss.OSS;
import com.aya.export.aspect.ExcelExportService;
import com.aya.export.aspect.ExportAspect;
import com.aya.export.config.AliyunOssConfig;
import com.aya.export.core.OssExporter;
import com.aya.export.core.StreamingExporter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import javax.servlet.http.HttpServletResponse;

@Configuration
@EnableAspectJAutoProxy  // 启用AspectJ自动代理‌
public class DownloaderAutoConfiguration {

    @Bean
    public ExportAspect exportAspect() {
        return new ExportAspect();  // 注册切面Bean
    }

    @Bean
    public ExcelExportService excelExportService() {
        return new ExcelExportService();  // 注册切面Bean
    }


    @Bean
    public StreamingExporter aliyunOssConfig(HttpServletResponse response) {
        return new StreamingExporter(response);
    }

    @Configuration
    @ConditionalOnClass(OSS.class)
    @ConditionalOnBean(AliyunOssConfig.class)
    public static class AliyunOssConfiguration {

        @ConditionalOnMissingBean(OssExporter.class)
        @Bean
        public OssExporter aliyunOssExporter(AliyunOssConfig aliyunOssConfig) {
            return new OssExporter(aliyunOssConfig);
        }
    }
}
